package com.qianya.system.manage.web.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.netflix.ribbon.proxy.annotation.Http;
import com.qianya.model.common.ResultCode;
import com.qianya.model.pojo.sys.qo.SysSetLimitIpQO;
import com.qianya.system.manage.web.sys.model.SysConfig;
import com.qianya.system.manage.web.sys.model.SysSetLimitIp;
import com.qianya.system.manage.web.sys.service.ISysConfigService;
import com.qianya.system.manage.web.sys.service.ISysSetLimitIpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.qianya.model.common.CommonResult;
import com.qianya.model.pojo.sys.qo.SysConfigQO;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统设置 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2020-06-11
 */
@RestController
@RequestMapping("/sys/sys-config")
@Api(value = "SysConfigController", tags = "系统设置-默认设置")
@Slf4j
public class SysConfigController {

    private Logger logger = LoggerFactory.getLogger(SysConfigController.class);

    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private ISysSetLimitIpService limitIpService;


    @Resource
    HttpServletRequest request;

    @PutMapping(value = "/update")
    @ApiOperation("修改系统设置")
    public CommonResult update(@RequestBody SysConfigQO sysConfigQO) {

        String ipList = sysConfigQO.getIpList();

        String[] split = ipList.split("--");

        String[] split1 = null;
        SysSetLimitIp sysSetFrontIp =null;
        ArrayList<SysSetLimitIp> limitIpArrayList = new ArrayList<>();

        for (int i = 0; i < split.length; i++) {
            sysSetFrontIp= new SysSetLimitIp();
            if (StrUtil.isBlank(split[i])){
                continue;
            }
            split1 = split[i].split("-");
            if (StrUtil.isBlank(split1[0]) || StrUtil.isBlank(split1[1])){
                continue;
            }
            sysSetFrontIp.setFiBegin(split1[0]);
            sysSetFrontIp.setFiEnd(split1[1]);
            limitIpArrayList.add(sysSetFrontIp);
        }


        limitIpService.remove(new QueryWrapper<SysSetLimitIp>().ne("FI_ID",""));

        if (limitIpArrayList.size()>0){

            limitIpService.saveBatch(limitIpArrayList);
        }
        limitIpService.ipLimitInit();
        SysConfig sysConfig = new SysConfig();

        BeanUtils.copyProperties(sysConfigQO, sysConfig);


        return sysConfigService.renewal(sysConfig);
    }

    @ApiOperation("查找系统设置")
    @GetMapping(value = "/find")
    public CommonResult find() {
        SysConfig sysConfig = (SysConfig) sysConfigService.find().getBean();
        sysConfig.setIpList(limitIpService.list());
        return CommonResult.success(sysConfig);
    }
}
